import { _decorator, assetManager, director, easing, Layout, Node, tween, v3, Vec3 } from 'cc';
import { Sprite } from 'cc';
import { SpriteFrame } from 'cc';
import { MonoUIPanel } from '../../../../common/src/mono/MonoUIPanel';
import { SETTING_TYPE } from '../../../../common/src/util/Enums';
import { AudioManager } from '../../../../common/src/manager/AudioManager';
import { VibrateManager } from '../../../../common/src/manager/VibrateManager';
import { UIManager } from '../../../../framework/ui/uimanager/UIManager';

const { ccclass, property } = _decorator;

const SETTINGCONFIG = {
    SWITCHDURATION: 0.2
}

@ccclass('SettingDialog')
export class SettingDialog extends MonoUIPanel {
    @property({ type: Layout, tooltip:"内容布局."})
    content: Layout = null;   

    @property({ type: Node, tooltip:"节点-声音."})
    soundNode: Node = null;

    @property({ type: Node, tooltip:"节点-震动."})
    vibrateNode: Node = null;

    @property({ type: Node, tooltip: "节点-音乐." })
    musicNode: Node = null;

    @property({ type: Node, tooltip: "按钮-首页." })
    btnHome: Node = null;

    @property({ type: Node, tooltip: "按钮-重开." })
    btnRestart: Node = null;
    
    
    @property({ type: Node, tooltip: "游戏时" })
    gameType: Node = null;
 
    @property({ type: SpriteFrame, tooltip: "打开条" })
    onSprite: SpriteFrame = null;
    
    @property({ type: SpriteFrame, tooltip: "关闭条" })
    offSprite: SpriteFrame = null;

    // =======================================
    // 外部/内部属性定义(以public/private修饰)
    // =======================================

    // 开关位置
    private tempOnPos = v3(-42, 0, 0)
    private tempOffPos = v3(42, 0, 0)

    private info: {
        type: SETTING_TYPE
    }

    // =======================================
    // 生命周期(模板方法，以on开头)
    // =======================================

    // =======================================
    // 游戏逻辑方法(内部调用的用private修饰，外部调用和编辑器绑定的public修饰，废弃的方法不加修饰符方便后期移除)
    // =======================================
    // ------------------------ 外部方法 ------------------------
    public async setData(data) {
        this.info = data
        this.initUI()
    }

    // ------------------------ 内部方法 ------------------------    
    /** 更新震动状态. */
    private updateVibrateState(isAnimation = false) {
        let onNode = this.vibrateNode.children[0]

        if (isAnimation) {
            if (VibrateManager.enable) {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOnPos }).start()
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOffPos }).start()
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (VibrateManager.enable) {
                onNode.position = this.tempOnPos
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.vibrateNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

    /** 更新声音状态. */
    private updateSoundState(isAnimation = false) {
        let onNode = this.soundNode.children[0]

        if (isAnimation) {
            if (AudioManager.enable) {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOnPos }).start()
                this.soundNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOffPos }).start()
                this.soundNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (AudioManager.enable) {
                onNode.position = this.tempOnPos
                this.soundNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.soundNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

    /** 更新音乐状态. */
    private updateMusicState(isAnimation = false) {
        let onNode = this.musicNode.children[0]

        if (isAnimation) {
            if (AudioManager.musicEnable) {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOnPos }).start()
                this.musicNode.getComponent(Sprite).spriteFrame = this.onSprite;
            }
            else {
                tween(onNode).to(SETTINGCONFIG.SWITCHDURATION, { position: this.tempOffPos }).start()
                this.musicNode.getComponent(Sprite).spriteFrame = this.offSprite;
            }
        } else {
            if (AudioManager.musicEnable) {
                onNode.position = this.tempOnPos
                this.musicNode.getComponent(Sprite).spriteFrame = this.onSprite
            } else {
                onNode.position = this.tempOffPos
                this.musicNode.getComponent(Sprite).spriteFrame = this.offSprite
            }
        }
    }

     /** 初始化UI. */
    private initUI() {
        if(this.info){
            if (this.info.type == SETTING_TYPE.START) {
                this.gameType.active = false
            } else if (this.info.type == SETTING_TYPE.GAME) {
                this.gameType.active = true
            }
        }

        this.updateSoundState();
        this.updateVibrateState();
        this.updateMusicState();
    }

    /** 关闭面板. */
    public async onClosePanel() {
        await this.onExitAnimation();
        UIManager.close("setting:res/prefab/SettingDialog");
    }

    //重新开始
    public onClickRestartButton() {
        
    }
    
    // =======================================
    // 自定义事件回调(以on开头)
    // =======================================
    /**关闭 */
    public onClickClose() {
        AudioManager.playButton();
        this.onClosePanel();
    }
 
     /**点击音效 */
     onClickSound() {
        AudioManager.enable = !AudioManager.enable;
        AudioManager.playButton();
        this.updateSoundState(true);
    }

    /**点击音乐 */
    onClickMusic() {
        AudioManager.playButton();
        AudioManager.musicEnable = !AudioManager.musicEnable;

        
        this.updateMusicState(true);
    }

    /**点击震动 */
    onClickShake() {
        AudioManager.playButton();
        VibrateManager.enable = !VibrateManager.enable;
        VibrateManager.vibrateShort();
        this.updateVibrateState(true);
    }

    /**回到主页 */
    onClickHome() {
        AudioManager.playButton();
        this.onClosePanel()
    
        director.loadScene("start")
    }

    /**重新游戏 */
    onClickRestartGame() {
        AudioManager.playButton();
        this.onClosePanel()
    }

    //退出游戏
    public onClickExitButton() {
        AudioManager.playButton();
        UIManager.open("exit:res/prefab/ExitDialog");
        this.onClosePanel();
    }
}


